﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Text;
using ReflectionStudio.Core.Events;
using INTEX.DataBase;
using INTEX.DataBase.Tools;
namespace WPFExcelReport
{
    //
    public class SDTAB0305Data : BaseFunction
    {

        INTEXDBC INTEXDBC;
        SETINTEXDB INTEXDB = new SETINTEXDB();
        public SDTAB0305Data()
            : base()
        {

        }
        public SDTAB0305Data(OracleConnection _OracleDB, IntexReptServBLClient _proxy, string userID = "        ")
            : base(_OracleDB, _proxy, userID)
        {
            INTEXDBC = new INTEXDBC(_OracleDB, _proxy);
        }
        public SDTAB0305Data(Dictionary<string, object> DicGlobalPara)
            : base(DicGlobalPara)
        {

        }
        public DataSet GetABPOS(DataRow dwABPOS)
        {
            DataSet Result;
            DataSet Return = new DataSet();
            string TableName = "ABPOS";
            string ABPAUFKRS = dwABPOS["ABPAUFKRS"].ToString();
            string ABPAUFLFD = dwABPOS["ABPAUFLFD"].ToString().Trim();
            string FACTORY = dwABPOS["ABPANRKR"].ToString().Trim();
            string STAFF = dwABPOS["ABPAENDBEN"].ToString().Trim();

            try
            {
                orlReadCommand = OracleDB.CreateCommand();
                orlReadCommand.CommandText = "SELECT TRIM(D.LSLIEFNA) AS LSLIEFNA,TRIM(D.LSNAME1) AS FACTORY,TRIM(C.ABKARTSB) AS ABKARTSB,TRIM(E.HRNNAME) AS STAFF, "
                    + " A.ABPAUFKRS,A.ABPAUFLFD,A.ABPAUFPOS, A.ABPART,A.ABPVAR,A.ABPFARBE,F.WAFBEZ AS COLOR, "
                    + " A.ABPBESCHICH,A.ABPSOLLMENGE1*0.01 AS ABPSOLLMENGE1,A.ABPSOLLMENGE5*0.01 AS ABPSOLLMENGE5, "
                    + " A.ABPSOLLMENGE7*0.01 AS ABPSOLLMENGE7, "
                    + " A.ABPSTARTDAT,A.ABPLIEFDAT,B.ABABEM1,B.ABABEM2  "
                    + " FROM ABPOS A, ABAUFM B,ABKOPF C,LIEFDAT D,HRSTAMM E,WAFARBEN F "
                    + " WHERE A.ABPMANDANT='00'  AND A.ABPERLKZ=' ' AND A.ABPMANDANT = B.ABAMANDANT "
                    + " AND A.ABPAUFKRS=B.ABAAUFKRS AND A.ABPAUFLFD=B.ABAAUFLFD AND A.ABPAUFPOS=B.ABAAUFPOS "
                    + " AND B.ABAAPOS='0001' AND A.ABPAUFART='VA' AND A.ABPMANDANT = C.ABKMANDANT "
                    + " AND A.ABPAUFKRS = C.ABKAUFKRS AND A.ABPAUFLFD = C.ABKAUFLFD  "
                    + " AND C.ABKMANDANT = E.HRMANDANT AND C.ABKARTSB = E.HRBENID "
                    + " AND A.ABPMANDANT = F.WAFMANDANT AND A.ABPART = F.WAFART "
                    + " AND A.ABPFARBE = F.WAFZUSATZ AND F.WAFZUART = 'F' "
                    + " AND A.ABPMANDANT = D.LSMANDANT AND A.ABPLIEFNR = D.LSLIEFNR "
                    + " AND (A.ABPAUFKRS = '" + ABPAUFKRS + "' OR '" + ABPAUFKRS + "' = '  ') "
                    + " AND (A.ABPAUFLFD = '" + ABPAUFLFD + "' OR '" + ABPAUFLFD + "' = '000000') "
                    + " AND (trim(D.LSLIEFNA) = '" + FACTORY + "' OR '" + FACTORY + "' = 'AA' ) "
                    + " AND (trim(C.ABKARTSB) = '" + STAFF + "' OR '" + STAFF + "' = 'AAAAAAAA' ) "
                    + " ORDER BY A.ABPAUFKRS,A.ABPAUFLFD,A.ABPAUFPOS "
                    ;

                Result = Connect2Proxy(orlReadCommand);
                if (Result.Tables.Count > 0)
                {
                    Result.Tables[0].TableName = TableName;
                    /*byte[] b;
                    string value;
                    foreach (DataRow dw in Result.Tables[TableName].Rows)
                    {
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dw["EKPLSART"].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dw["EKPLSART"] = value;
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dw["EKPLSFB"].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dw["EKPLSFB"] = value;
                    }*/
                }
                Return.Merge(Result);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return Return;
        }

        public DataSet BuildData(DataRow dwInput)
        {
            DataSet Result = new DataSet();
            DataSet Return = new DataSet();
            try
            {
                Result = INTEXDBC.QueryABPOS(dwInput, 4);
                Result = GetABPOS(dwInput);
                Return.Merge(Result);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return Return;
        }
        public void UpdateABAUFM(DataRow dwInput)
        { 
            INTEXDBC.UpdateABAUFM(dwInput, 1, "50.24"); 
        }
    }
}